/**
 * @author ccpalu
 */
 function addScript(src,id){
 	var headID = document.getElementsByTagName("head")[0];
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = src;
	headID.appendChild(script); // retrieve answer
	
	return false;
}

function addLink(href,rel){
	var headID = document.getElementsByTagName("head")[0];
	var script = document.createElement('link');
	script.type = 'text/css';
	script.rel = rel;
	script.href = href;
	headID.appendChild(script); // retrieve answer

	setTimeout('loadBrowser(0)',250);
	return false;
	
}

function getUrlVars(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&&&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('===');
		hash[1] = unescape(hash[1]);
		vars.push(hash[0]);
	
        vars[hash[0]] = hash[1];
    }

    return vars;
}

function setPosition (obj, l, t, position){
	try {
		if (typeof obj == 'string') 
			obj = document.getElementById(obj);
		
		if ((obj.tagName == 'P') && ((window.navigator.appName == 'Netscape') || (window.navigator.appName == 'Opera'))) {
			t = t - 15;
		}
		if (position) 
			obj.style.position = position;
		else 
			obj.style.position = "absolute";
		if (l) 
			obj.style.left = l + "px";
		if (t) 
			obj.style.top = t + "px";
	}
	catch(e){
		var a='x';
	}
	
	return false;

}

function resize(obj, width){
	var w = obj.width;
	var h = obj.height;
	var rate = width/w;
	obj.width=width;

	if (navigator.appName=='Microsoft Internet Explorer'){
		obj.style.height=h*rate;
	}
	
	return false;
}

function addHelp (id, alt, url, parentId, t, l, w, h){
	/*
	 *url="http://sites.google.com/site/cnviewerguide/home/input-files/loading-data"
	 *addHelp ('hUpload', 'help', url, 'sourceDiv', 10, 250) 
	 * 
	 */
	var bH=document.createElement('IMG');
	document.getElementById(parentId).appendChild(bH);

	bH.id=id;
	bH.src="http://cnviewer.googlecode.com/hg/Images/int.png";
	bH.url=url;
	bH.onclick=function(){
		var gbPopup = window.open(this.url);

		//Checking if the pop-up blocker is disabled
		if (gbPopup == null || typeof(gbPopup) == 'undefined'){
			alert ('Please disable your pop-up blocker and click at the "UCSC Genome Browser" again.');
		}
		else{
			gbPopup.focus();
		}

		return false;
	};
	
	bH.style.cursor='pointer';
	bH.alt = alt;
	bH.title = alt;

	bH.style.position = 'absolute';
	if (w) 
		resize(bH, w);
	if (h) 
		bH.style.height = h + 'px';

	bH.style.left = l + 'px';
	bH.style.top = t + 'px';
	
	//	return bI;
	
		return false;
}

/*function go2help(){
	var gbPopup = window.open(url);

	//Checking if the pop-up blocker is disabled
	if (gbPopup == null || typeof(gbPopup) == 'undefined'){
		alert ('Please disable your pop-up blocker and click at the "UCSC Genome Browser" again.');
	}
	else{
		gbPopup.focus();
	}

	return false;
}*/

function setFont(obj,size,st, align, wheight,transform){
	if (typeof obj == 'string')
		obj = document.getElementById(obj);
	obj.style.fontFamily = "arial,sans-serif";
	obj.style.fontSize=size;
	if (st)
		obj.style.fontStyle=st;
	if (align)
		obj.style.textAlign=align;
	if (wheight)
		obj.style.fontWeight=wheight;
	if (transform)
		obj.style.textTransform=transform;
	return false;
}
	
function numberComma(number){
	
	var numberTemporary=String(number);
	var point=numberTemporary.indexOf('.');
	var numberSize=numberTemporary.length;
	var howManyCommas=Math.floor(numberSize/3);
	var numberWithComma='';
	
	if (point!='-1'){
		for (var aux = point; aux <= numberSize - 1; aux += 1) {
			numberWithComma = numberWithComma + numberTemporary.charAt(aux);
		}
		aux=point-1;
	}
	else
		var aux=numberSize-1;
		
	for (var comma=1; aux>=0; aux-=1, comma+=1){
		numberWithComma=numberTemporary.charAt(aux)+numberWithComma;
		if (((comma%3)==0)&&(aux!=0)&&(numberTemporary.charAt(aux)!='-')){
			numberWithComma=','+numberWithComma;
		}
	}
	
	return numberWithComma;
}

function loadingStatus(message){
	
	remove_element_by_id('LoadingStatus');
 
	var lStatus = document.createElement('p');
	lStatus.id = 'LoadingStatus';
	lStatus.name = 'LoadingStatus';
	lStatus.innerHTML = message;
	setFont(lStatus, "13px", "italic");
	setPosition(lStatus, 15, 175);
	document.body.appendChild(lStatus);

	var load=document.createElement('IMG');
	load.id='Loding';
	load.src='ajax-loader.gif';
	load.width=30;
	load.height=30;
	setPosition(load,160,-2);
	document.getElementById('LoadingStatus').appendChild(load);

	return false;
}

function findInArray(arr,target,subnode){
	if (!subnode) {
		for (var i = 0; i < arr.length; i = i + 1) {
			if (arr[i] == target) 
				return i;
		}
	}
	else{
		for (var i = 0; i < arr.length; i = i + 1) {
			if (arr[i][subnode] == target) 
				return i;
		}
	}
	return -1;
}

function findInArraySorted(arr, subnode){

	this.id = 'findObj';
	this.start = 0;
	this.end = arr.length - 1;

	if (!subnode) 
		this.subnode = false;
	else 
		this.subnode = subnode;
	this.arr = arr;
	
	this.find=function(target){
		this.target = target;

		var pivot=Math.floor((this.end+this.start)/2);

	
		if (!this.subnode) {
			if (this.target == this.arr[pivot]) {
				this.start = 0;
				this.end = this.arr.length - 1;
		
				return pivot;
			}
			else{
				switch (pivot){
				case 0:
					this.start = 0;
					this.end = this.arr.length - 1;
					
					return -1;
				break;
				case 1:
					if (this.target == this.arr[pivot - 1]) {
						this.start = 0;
						this.end = this.arr.length - 1;
						
						return pivot - 1;
					}
					else {
						this.start = 0;
						this.end = this.arr.length - 1;
						
						return -1;
					}
				break;
				default:
				
					if(this.target<this.arr[pivot])
						this.end=pivot-1;
					else
						this.start=pivot+1;
						
					return this.find(target);
				}
			}
		}
		else{
			if (this.target == this.arr[pivot][this.subnode]) {
				this.start = 0;
				this.end = this.arr.length - 1;
				
				return pivot;
			}
			else{
				switch (pivot){
				case 0:
					this.start = 0;
					this.end = this.arr.length - 1;

					return -1;
				break;
				case 1:
					if (this.target == this.arr[pivot - 1][this.subnode]) {
						this.start = 0;
						this.end = this.arr.length - 1;
						
						return pivot - 1;
					}
					else {
						this.start = 0;
						this.end = this.arr.length - 1;
						
						return -1;
					}
				break;
				default:
				
					if(this.target<this.arr[pivot][this.subnode])
						this.end=pivot-1;
					else
						this.start=pivot+1;
						
					return this.find(target);
				}
			}				
		}
	};
	this.equalOrBiggerValue=function(target){
		this.target = target;

		var pivot=Math.floor((this.end+this.start)/2);

	
		if (!this.subnode) {
			if (this.target == this.arr[pivot]) {
				this.start = 0;
				this.end = this.arr.length - 1;
		
				return pivot;
			}
			else{
				switch (this.end-this.start){
				case 0:
					this.start = 0;
					this.end = this.arr.length - 1;
					
						return pivot;
					
				break;
				case 1:
						this.start = 0;
						this.end = this.arr.length - 1;	
						
					if (this.target <= this.arr[pivot - 1]) 
						return pivot-1;				
					else{
						if (this.arr[pivot]>this.target){
							return pivot;
						} 
						else
							return pivot+1;
					}
					 
					
				break;
				default:
					
					if (this.target < this.arr[pivot]) {
						if (this.target > this.arr[pivot - 1]) {
							this.start = 0;
							this.end = this.arr.length - 1;
							return pivot;
						}
						else
							this.end = pivot - 1;
					}
					else 
						this.start = pivot + 1;
						
					return this.equalOrBiggerValue(target);
				}
			}
		}
		else{
			if (this.target == this.arr[pivot][this.subnode]) {
				this.start = 0;
				this.end = this.arr.length - 1;
				
				return pivot;
			}
			else{
				switch (pivot){
				case 0:
					this.start = 0;
					this.end = this.arr.length - 1;
			
//					if (this.arr[pivot][this.subnode]this.target)
						return pivot;
	//				else
		//				return pivot+1;
				break;
				case 1:
					this.start = 0;
					this.end = this.arr.length - 1;	
						
					if (this.target <= this.arr[pivot - 1][this.subnode]) 
						return pivot-1;				
					
					else{
						if (this.arr[pivot][this.subnode]>this.target){
							return pivot;
						} 
						else
							return pivot+1;
					}
					
				break;
				default:
				
					if (this.target < this.arr[pivot][this.subnode]) {
						if (this.target > this.arr[pivot - 1][this.subnode]) {
							this.start = 0;
							this.end = this.arr.length - 1;
							return pivot;
						}
						else
							this.end = pivot - 1;
					}
					else 
						this.start = pivot + 1;
						
					return this.equalOrBiggerValue(target);
					
				}
			}				
		}
	};
	this.equalOrSmallerValue=function(target){
		this.target = target;

		var pivot=Math.floor((this.end+this.start)/2);

	
		if (!this.subnode) {
			if (this.target == this.arr[pivot]) {
				this.start = 0;
				this.end = this.arr.length - 1;
		
				return pivot;
			}
			else{
				switch (this.end-this.start){
				case 0:
					this.start = 0;
					this.end = this.arr.length - 1;
					
				//	if (this.arr[pivot]<this.target)
						return pivot;
				//	else
					//	return pivot-1;

				break;
				case 1:
						this.start = 0;
						this.end = this.arr.length - 1;	
						
					if (this.target >= this.arr[pivot]) {
						if (this.target >= this.arr[pivot + 1]) 
							return pivot + 1;
						else 
							return pivot;
					}
					else {
					if (this.pivot)
						return pivot - 1;
					else
						return 0;
					 }
					
				break;
				default:
					
					if ( this.target > this.arr[pivot]) {
						if (this.target < this.arr[pivot + 1]) {
							this.start = 0;
							this.end = this.arr.length - 1;
							return pivot;
						}
						else
							this.start = pivot + 1;
					}
					else 
						this.end = pivot - 1;
						
					return this.equalOrSmallerValue(target);
				}
			}
		}
		else{
			if (this.target == this.arr[pivot][this.subnode]) {
				this.start = 0;
				this.end = this.arr.length - 1;
		
				return pivot;
			}
			else{
				switch (this.end-this.start){
				case 0:
					this.start = 0;
					this.end = this.arr.length - 1;
					
				//	if (this.arr[pivot]<this.target)
						return pivot;
				//	else
					//	return pivot-1;

				break;
				case 1:
						this.start = 0;
						this.end = this.arr.length - 1;	
						
					if (this.target >= this.arr[pivot][this.subnode]) 
						return pivot;				
					else
						return pivot-1;
					 
					
				break;
				default:
					
				/*	if (this.target < this.arr[pivot][this.subnode]) {
						if (this.target > this.arr[pivot - 1]) {
							this.start = 0;
							this.end = this.arr.length - 1;
							return pivot-1;
						}
						else
							this.end = pivot - 1;
					}
					else 
						this.start = pivot + 1;
						
					return this.equalOrSmallerValue(target);*/
					if ( this.target > this.arr[pivot][this.subnode]) {
						if (this.target < this.arr[pivot + 1][this.subnode]) {
							this.start = 0;
							this.end = this.arr.length - 1;
							return pivot;
						}
						else
							this.start = pivot + 1;
					}
					else 
						this.end = pivot - 1;
						
					return this.equalOrSmallerValue(target);
				}
			}
		}
	};
	
	return false;
}

function minArray1(arr,subnodes){
	var min='';
	var index=0;
	
	if (!subnodes) {
		min=arr[0];
		for (var i = 1; i < arr.length; i = i + 1) {
			if (arr[i] < min) {
				min=arr[i];
				index = i;
			}
		}
	}
	else{
		min=eval ( 'arr[0]'+subnodes);
		for (var i = 1; i < arr.length; i = i + 1) {
			if ((eval ( "arr["+i+"]"+subnodes)) < min) {
				min=eval ( "arr["+i+"]"+subnodes);
				index = i;
			}
		}
	}
	
	return min;//[min, index];
}

function maxArray(arr,subnode){
	var max='';
	var index=0;
	
	if (!subnode) {
		max=arr[0];
		for (var i = 1; i < arr.length; i = i + 1) {
			if (arr[i] < max) {
				max = arr[i];
				index = i;
			}
		}
	}
	else{
		var nodes='';
		for (var i=0;i < subnode.length; i=i+1){
			if (subnode[i]=='last')
				nodes=nodes+'[eval( "arr["+i+"]")' + nodes +'.length-1]';
				
			else
				nodes=nodes+"['"+subnode[i]+"']";
		}
		i=0;
		max=eval ( 'arr[0]'+nodes);
		for (var i = 1; i < arr.length; i = i + 1) {
			if ((eval ( "arr["+i+"]"+nodes)) > max) {
				max=eval ( "arr["+i+"]"+nodes);
				index = i;
			}
		}
	}
	
	return max;//[max, index];
}

function minArray(arr,subnode){
	var min='';
	
	if (!subnode) {
		min=arr[0];
		for (var i = 1; i < arr.length; i = i + 1) {
			if (arr[i] < min) 
				min=arr[i];
		}
	}
	else{
		var nodes='';
		for (var i=0;i < subnode.length; i=i+1){
			nodes=nodes+"['"+subnode[i]+"']";
		}
		
		min=eval ( 'arr[0]'+nodes);
		for (var i = 1; i < arr.length; i = i + 1) {
			if ((eval ( "arr["+i+"]"+nodes)) < min) 
				min=eval ( "arr["+i+"]"+nodes);
		}
	}
	
	return [min, i];
}

function createParagraph(id, text, parentObj, align){
	var newP = document.createElement("p");
	newP.id = id;
	newP.innerHTML = text;
	if (align)
		newP.align=align;
	
	try {
		switch (typeof parentObj) {
			case 'object':
				parentObj.appendChild(newP);
				break;
			case 'function':
				parentObj.appendChild(newP);
				break;
			case 'string':
				document.getElementById(parentObj).appendChild(newP);
				break;
			default:
				document.getElementsByTagName('body')[0].appendChild(newP);
		}
	}
	catch (e){	}
	
	return newP;
}

function revival(id){
	var clone = document.getElementById(id);
	remove_element_by_id(clone.id);
	document.body.appendChild(clone);
	
	return false;
}

function buttonImage(id, file, func, w, h, l, t, position, parentObj){
	
	var bI=document.createElement('DIV');
	bI.id=id;
	var inner = '<img id="'+id+'Img" src="' + file + '" onclick="'+func+'" style="cursor:pointer';
	if (position)
		inner=inner+'; position:'+position;
	else
		inner=inner+'; position:absolute';
	
	if (w)
		inner=inner+'; width:'+w+'px';
	if (h)
		inner=inner+'; height:'+h+'px';
	if (l)
		inner=inner+'; left:'+l+'px';
	if (t)
		inner=inner+'; top:'+t+'px';
		
	
	inner = inner + '" ></img>';
	
	bI.innerHTML=inner;
	
	if (parentObj)
		parentObj.appendChild(bI);
	else
		document.body.appendChild(bI);

	return bI;
}

function createInput(id, type, value, func,parentObj, l, t, disabled){
	var input = document.createElement('INPUT');
	input.type = type;
	input.value = value;
	if(func)
		input.onclick = func;

	if (typeof parentObj== 'string')
		parentObj = document.getElementById(parentObj);

	 parentObj.appendChild.input;
	 if (l||t)
	 	setPosition(input, l, t);
	 if (disabled)
	 	input.disabled=true;
	 
	return input;
	
}

function copyArray(arr){
	var copy = new Array(arr.length);
	
	for (var i=0; i< copy.length; i =i +1){
		copy[i]= arr[i];
	}
	
	return copy;
}

function DOJOrequires(){
	dojo.require("dojox.gfx");
	dojo.require("dojox.gfx.MoveableLimited");
	//dojo.require("dojox.charting.Chart2D");
	//dojo.require("dojox.charting.Theme");
	//dojo.require("dojox.charting.themes.PlotKit.blue");
	//dojo.require("dojox.charting.action2d.Highlight");
	//dojo.require("dojox.charting.action2d.Magnify");
	//dojo.require("dojox.charting.action2d.Shake");
	//dojo.require("dojox.charting.action2d.Tooltip");
	dojo.require("dojox.charting.Theme");
	//dojo.require("dojox.charting.themes.PlotKit.cyan");
	//dojo.require("dojox.charting.action2d.Shake");
	
	return false;
}

function cleanObject(obj){
	if (typeof obj == 'string') 
			obj = document.getElementById(obj);
			
	while (obj.children.length>0){
		obj.removeChild(obj.children[0]);
	}
	return false;
}
